package com.team.six.bluecatshop.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.team.six.bluecatshop.domain.*;
import com.team.six.bluecatshop.mapper.AddressMapper;
import com.team.six.bluecatshop.mapper.CustomerMapper;
import com.team.six.bluecatshop.mapper.IndentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class IndentServiceImpl implements IndentService {
    @Autowired
    private IndentMapper indentMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private AddressMapper addressMapper;

    @Override
    @Transactional
    public void add(Indent indent) {
        indentMapper.insert(indent);
    }

    @Override
    @Transactional
    public void remove(String id) {
        indentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Indent findById(String id) {
        Indent indent=indentMapper.selectByPrimaryKey(id);
        return indent;
    }

    @Override
    public List<Indent> findAll() {
        List<Indent> indentList = indentMapper.findAll();
        return indentList;
    }

    @Override
    public List<Indent> findByCustomerIdAndIdentify1(String id) {
        return indentMapper.findByCustomerIdAndIdentify1(id);
    }

    @Override
    public List<Indent> findByCustomerIdAndIdentify0(String id) {
        return indentMapper.findByCustomerIdAndIdentify0(id);
    }


    @Override
    @Transactional
    public void updateByPrimaryKey(Indent indent) {
        indentMapper.updateByPrimaryKeySelective(indent);
    }

    @Override
    public void updateIndentByIndentId(Indent indent) {
        indentMapper.updateByPrimaryKeySelective(indent);
    }

    @Override
    public SeachIndentResponse findIndentAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        IndentExample example = new IndentExample();
        List<Indent> indents = indentMapper.selectByExample(example);
        List<Indent> newIndents = new ArrayList<>();
        for (Indent indent : indents) {
            Customer customer = customerMapper.selectByPrimaryKey(indent.getCustomerId());
            indent.setIndentCustomerName(customer.getCustomerName());
            Address address = addressMapper.selectByPrimaryKey(indent.getAddressId());
            indent.setIndentAddress(address.getAddress());
            if(indent.getIndentIdentify() == 0){
                indent.setIndentIdentification("未付款");
            }else{
                indent.setIndentIdentification("已付款");
            }
            newIndents.add(indent);
        }
        return new SeachIndentResponse(((Page)indents).getTotal(),newIndents);
    }
}
